package com.njwb.day15.service.impl;

import com.njwb.day15.dao.StudentDao;
import com.njwb.day15.dao.impl.StudentDaoImpl;
import com.njwb.day15.pojo.Student;
import com.njwb.day15.service.StudentService;
import com.njwb.day15.util.DbUtil;

import java.sql.SQLException;
import java.util.List;

public class StudentServiceImpl  implements StudentService {
    private StudentDao studentDao = new StudentDaoImpl() ;


    @Override
    public int addStudent(Student stu) {
        try {
            return studentDao.addStudent(stu);
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }finally {
            try {
                DbUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int updateStudent(Student stu) {
        try {
            return studentDao.updateStudent(stu);
        } catch (SQLException e) {
            e.printStackTrace();
            return -1 ;
        }finally {
            try {
                DbUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int deleteStudentById(int sid) {
        try {
            return studentDao.deleteStudentById(sid);
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }finally {
            try {
                DbUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Student findStudentById(int sid) {
        try {
            return studentDao.findStudentById(sid);
        } catch (SQLException e) {
            e.printStackTrace();
            return null ;
        }finally {
            try {
                DbUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Student> queryAllStudents() {
        try {
            return studentDao.queryAllStudents();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            try {
                DbUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
